package StringType;

import java.util.ArrayList;

/**
 * @Author ChanZany
 * @Date 2021/5/14 15:04
 * @Version 1.0
 * 就是给定一个字符串，然后按写竖着的 「z」的方式排列字符，就是下边的样子。
 * a     g
 * b   f h
 * c e   i
 * d     j
 * 解法：
 */
public class ZigZagConversion {
    public static void main(String[] args) {
        String s = "abcdefghij";
        int numberRows = 4;
        String res = convert(s,numberRows);
        System.out.println(res);
    }

    private static String convert(String s, int numberRows) {
        ArrayList<StringBuilder> builders = new ArrayList<>();
        for (int i = 0; i < numberRows; i++) {
            builders.add(new StringBuilder());
        }
        if (s.length()<=numberRows||numberRows==1) return s;
        char[] chars = s.toCharArray();
        boolean goDown = false;
        int curRow = 0;
        for (char c : chars) {
            if (curRow==numberRows-1||curRow==0) goDown=!goDown;
            builders.get(curRow).append(c);
            curRow += goDown?1:-1;
        }
        StringBuilder res = new StringBuilder();
        for (StringBuilder builder : builders) {
            System.out.println(builder.toString());
            res.append(builder.toString());
        }
        return res.toString();
    }


}
